package com.hr.user.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;

import com.hr.utils.DownUtils;

public class DownloadFileServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("text/html;charset=UTF-8");
		String filename = request.getParameter("filename");
		String path = request.getParameter("path");
		String[] strArr = path.split("/");
		String framename = DownUtils.filenameEncoding(strArr[strArr.length-1], request);
		String contentType = this.getServletContext().getMimeType(path);
		String contentDisposition = "attachment;filename="+framename;
		//一个流
		File file = new File(this.getServletContext().getRealPath("/"+path));
		
		FileInputStream fin = new FileInputStream(file);
		//设置两个头
		response.setHeader("Content-Type", contentType);
		response.setHeader("Content-Disposition", contentDisposition);
		ServletOutputStream output = response.getOutputStream();
		
		IOUtils.copy(fin, output);//把输入流数据写给output
		
		fin.close();
	}
	
}
